//
//  XHomeRecommendedCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/17.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XHomeRecommendedCell.h"
#import "LMJElementsFlowLayout.h"
#import "FoundModel.h"
@interface XHomeRecommendedCell()<LMJElementsFlowLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView*collectionView;
@end
@implementation XHomeRecommendedCell

- (void)setData:(NSArray<FoundRecommendModel*>*)data{
    _data = data;
    [self.collectionView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setupUI{
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.mas_equalTo(0);
    }];
}

#pragma mark - UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.data.count>8?8:self.data.count;
//    return self.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    
    if (![cell.contentView viewWithTag:100]) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.tag = 100;
        label.textColor = UIColorHex(000000);
        label.font = kDefaultFont(13);
        label.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.bottom.mas_equalTo(AdaptedWidth(-10.0f));
        }];
    }
    if (![cell.contentView viewWithTag:101]) {
        UIImageView *iconV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, AdaptedWidth(60.0f), AdaptedWidth(60.0f))];
        iconV.tag = 101;
//        iconV.contentMode = UIViewContentModeScaleAspectFill;
        [cell.contentView addSubview:iconV];
        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
            make.height.equalTo(iconV.mas_width);
        }];
        if (self.hasLine) {
            ViewBorderRadius(iconV, AdaptedWidth(13.0f), 1, kLineColor);
        }else{
            ViewRadius(iconV, AdaptedWidth(13.0f));
        }
    }
    FoundRecommendModel*model = [self.data objectAtIndex:indexPath.item];
    UILabel *label = [cell.contentView viewWithTag:100];
    UIImageView *imgV = [cell.contentView viewWithTag:101];
    label.text = model.name;
    if (model.logoImg) {
        imgV.image = model.logoImg;
    }else{
        [imgV sd_setImageWithURL:String_To_URL(model.logo)];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    !self.ClickBlock?:self.ClickBlock([self.data objectAtIndex:indexPath.item]);
    !self.ClickIndexBlock?:self.ClickIndexBlock(indexPath.item);
}
#pragma mark - LMJElementsFlowLayoutDelegate

- (CGSize)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kHomeRecommendedWidth, kHomeRecommendedHeight);
}

- (UIEdgeInsets)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout edgeInsetsInCollectionView:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(10,AdaptedWidth(27.0f),10,AdaptedWidth(-27.0f));
}

- (CGFloat)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView columnsMarginForItemAtIndexPath:(NSIndexPath *)indexPath{
    return AdaptedWidth(19.0f);
}
- (CGFloat)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView linesMarginForItemAtIndexPath:(NSIndexPath *)indexPath{
    return 0.0f;
}


- (UICollectionView*)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[LMJElementsFlowLayout flowLayoutWithDelegate:self]];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
